﻿using System.Runtime.Serialization;

namespace DotNetHackathon.GBA.ApiCommon
{
    [Serializable]
    public class BusinessException : Exception,
        IHasErrorCode
    {
        /// <summary>
        /// 错误码
        /// </summary>
        public string? Code { get; set; }

        /// <summary>
        /// 构建业务异常
        /// </summary>
        /// <param name="code">错误码</param>
        /// <param name="message">错误信息</param>
        /// <param name="innerException">内部异常</param>
        /// <param name="logLevel">日志等级</param>
        public BusinessException(
            string? code = null,
            string? message = null,
            Exception? innerException = null)
            : base(message, innerException)
        {
            Code = code;
        }

        public BusinessException(SerializationInfo serializationInfo, StreamingContext context)
            : base(serializationInfo, context)
        { }

        /// <summary>
        /// 携带数据
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public BusinessException WithData(string name, object value)
        {
            Data[name] = value;
            return this;
        }
    }
}
